package com.example.extra.service.lmpl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.extra.entity.SysTypeVideo;
import com.example.extra.mapper.SysTypeVideoMapper;
import com.example.extra.utils.StringUtils;
import com.example.extra.vo.SysTypeVideoVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;


/**
 * @author lixiang
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class VideoTypeServiceIml {

    @Value("${file.prefix}")
    private String prefix;

    private final SysTypeVideoServiceImpl sysTypeVideoService;
    private final SysTypeVideoMapper sysTypeVideoMapper;

    public List<SysTypeVideoVo> getVideoList(String type, String name){
        LambdaQueryChainWrapper<SysTypeVideo> query = sysTypeVideoService.lambdaQuery();
        if(StringUtils.isNotEmpty(type)){
            query.eq(SysTypeVideo::getType,type);
        }
        if(StringUtils.isNotEmpty(name)){
            query.like(SysTypeVideo::getName,name);
        }
        List<SysTypeVideo> list = query.list();
        return Optional.ofNullable(list).map(p-> p.stream().map(map -> {
            map.setPath(map.getPath());
            SysTypeVideoVo vo = BeanUtil.copyProperties(map, SysTypeVideoVo.class);
            return vo;
        }).toList()).get();
    }
    public Page<SysTypeVideoVo> queryPageVideoList(String type, String name, int page, int size) {
        // 创建分页对象，注意page参数从1开始
        Page<SysTypeVideo> videoPage = new Page<>(page, size);
        
        // 创建查询条件
        LambdaQueryWrapper<SysTypeVideo> queryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotEmpty(type)){
            queryWrapper.eq(SysTypeVideo::getType, type);
        }
        if(StringUtils.isNotEmpty(name)){
            queryWrapper.like(SysTypeVideo::getName, name);
        }
        
        // 执行分页查询
        Page<SysTypeVideo> result = sysTypeVideoMapper.selectPage(videoPage, queryWrapper);
        
        // 转换为VO分页对象
        Page<SysTypeVideoVo> voPage = new Page<>(result.getCurrent(), result.getSize(), result.getTotal());
        result.getRecords().forEach(sysTypeVideo -> sysTypeVideo.setPath(sysTypeVideo.getPath()));
        voPage.setRecords(BeanUtil.copyToList(result.getRecords(), SysTypeVideoVo.class));
        return voPage;
    }
}
